package com.sqx.modules.log.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sqx.common.utils.Result;
import com.sqx.modules.log.dao.SysLogOperationDao;
import com.sqx.modules.log.entity.SysLogOperationEntity;
import com.sqx.modules.log.service.SysLogOperationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 操作日志
 *
 * @author fang
 *
 */
@Service
public class SysLogOperationServiceImpl extends ServiceImpl<SysLogOperationDao, SysLogOperationEntity> implements SysLogOperationService {

    @Override
    public Result page(JSONObject params) {
        Integer pageNum = params.getInteger("pageNum");
        Integer pageSize = params.getInteger("pageSize");
        PageHelper.startPage(pageNum,pageSize);
        List<Map<String, Object>> maps = baseMapper.queryByParams(params.getString("userName"), params.getString("ip"),
                params.getInteger("status"), params.getLong("userId"),params.getString("userType"));
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(maps);
        return Result.success().put("data",mapPageInfo);
    }

    @Override
    public List<SysLogOperationEntity> list(Map<String, Object> params) {
        List<SysLogOperationEntity> entityList = baseMapper.selectList(getWrapper(params));

        return entityList;
    }

    private QueryWrapper<SysLogOperationEntity> getWrapper(Map<String, Object> params){
        String status = (String) params.get("status");

        QueryWrapper<SysLogOperationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(status), "status", status);

        return wrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSysLog(SysLogOperationEntity entity) {
        baseMapper.insert(entity);
    }

}